<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyBranch extends BaseModel
{
    protected $name = 'sky_branch';

    public function detail($ancestor_id){
        $where = [
            'ancestor_id' => $ancestor_id,
        ];

        $list = $this->field('id,ancestor_name,total,branch,templateId')->cache(true)
            ->where($where)->select()->toArray();

        $good_relation_img = (new SkyGoods())->cache(true)->column('relation_img,img_url as icon','id');
        array_walk($good_relation_img,function(&$value,$key){
            $value['relation_img'] = json_decode($value['relation_img'],true);
        });


        array_walk($list, function (&$item, $index) use ($good_relation_img){
            $item['total']  = json_decode($item['total'],true);
            $branch = json_decode($item['branch'],true);
            foreach($branch as $p => $q){
                $branch[$p]['images'] = !empty($q['good_id'])?$good_relation_img[$q['good_id']]['relation_img']:[];
                $branch[$p]['icon'] = !empty($q['good_id'])?$good_relation_img[$q['good_id']]['icon']:$q['icon'];
                if(!empty($q['left'])){
                    foreach($q['left'] as $p1 => $q1){
                        $branch[$p]['left'][$p1]['images'] = !empty($q1['good_id'])?$good_relation_img[$q1['good_id']]['relation_img']:[];
                        $branch[$p]['left'][$p1]['icon'] = !empty($q1['good_id'])?$good_relation_img[$q1['good_id']]['icon']:$q1['icon'];
                    }
                }
                if(!empty($q['right'])){
                    foreach($q['right'] as $p1 => $q1){
                        $branch[$p]['right'][$p1]['images'] = !empty($q1['good_id'])?$good_relation_img[$q1['good_id']]['relation_img']:[];
                        $branch[$p]['right'][$p1]['icon'] = !empty($q1['good_id'])?$good_relation_img[$q1['good_id']]['icon']:$q1['icon'];
                    }
                }

            }
            $item['branch'] = $branch;
        });



        return $list;
    }

}